# -*- coding: utf-8 -*-
# @Time    : 2020/11/4 13:54
# @Author  : Mr. Cui
# @File    : client_api.py
# @Software: PyCharm
from application import db
from application.models.models import Report


def sub_report_service(task_id, build_id, case_id, type, total, succ, fail, err, info=None):
    """

    :param task_id:
    :param build_id:
    :param case_id:
    :param type: 1:web_ui 2:app_ui 3:接口
    :param total:
    :param succ:
    :param fail:
    :param err:
    :param info:详细报告地址
    :return:
    """
    insert = Report(task_id=task_id, build_id=build_id, case_id=case_id, type=type, total=total, succ=succ, fail=fail,
                    err=err, info=info)
    try:
        db.session.add(insert)
        db.session.commit()
        return True
    except Exception as e:
        print(e)
        db.session.rollback()
        return False


def update_report_service(task_id, build_id, case_id, type, total, succ, fail, err, info=None):
    """
    :param task_id:
    :param build_id:
    :param case_id:
    :param type: 1:web_ui 2:app_ui 3:接口
    :param total:
    :param succ:
    :param fail:
    :param err:
    :param info:详细报告地址
    :return:
    """
    # insert = Report(task_id=task_id, build_id=build_id, case_id=case_id, type=type, total=total, succ=succ, fail=fail,
    #                 err=err, info=info)
    try:
        db.session.query(Report).filter(Report.task_id == task_id, Report.build_id == build_id,
                                        Report.case_id == case_id,
                                        Report.type == type).update(
            {'total': total, 'succ': succ, 'fail': fail, 'err': err, 'info': info})

        db.session.commit()
        return True
    except Exception as e:
        print(e)
        db.session.rollback()
        return False
